﻿/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：chenkai
 * 日  期：2021-03-29
 * 描  述：通用项目选择
 */

var callType = request('callType');//1：流程界面直接选择，2：弹框中再选择
var textEleId = request('textEleId');//项目名称元素Id
var valueEleId = request('valueEleId');//项目Id元素Id
var ProjectId = request('ProjectId');//项目ID 页面传递
var initValue = request('initValue');//初始化选择的项目
var acceptClick;//结束回调方法;

//局部事件
var refreshGirdData;

var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            if (callType != "1" && callType != "2") {
                learun.alert.error("callType参数异常");
                return false;
            }
            page.bind();
        },
        bind: function () {
            if (initValue != "") {
                $("#ProjectId").val(initValue);
                learun.httpAsync('GET', top.$.rootUrl + '/ZZDT_EC/ec_project/GetFormData', { keyValue: initValue }, function (data) {
                    if (!!data) {
                        $("#ProjectName").val(data.ec_project.ProjectName);
                    }
                });
            }
            page.initGrid();

            // 查询
            $('#btn_Search').on('click', function () {
                page.search($('.lr-layout-tool-left').lrGetFormData());
            });

            //取消选择
            $("#btn_Remove").on('click', function () {
                $("#ProjectId").val("");
                $("#ProjectName").val("");
            });
        },
        //初始化列表
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_project/GetProjectSelectPageList',
                headData: [
                    { label: "项目名称", name: "ProjectName", width: 300, align: "left", sort: false }
                ],
                isPage: true,
                reloadSelected: true,
                mainId: 'ProjectId',
                sidx: "CreateTime ",
                sord: "DESC",
                onRenderComplete: function (data) {
                    //加载完毕逻辑
                    if (initValue != "") {
                        $('#gridtable').jfGridSet("selectRow", initValue);//初始化选中待优化
                    }
                },
                onSelectRow: function (row) { //单击选择行
                    $("#ProjectId").val(row.ProjectId);
                    $("#ProjectName").val(row.ProjectName);
                }
            });
            page.search();
        },
        //查询
        search: function (param) {
            param = param || {};
            param.ProjectId = initValue;
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };
    //刷新列表
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    //选中点击确定
    acceptClick = function (callBack) {
        var SelectProjectId = $("#ProjectId").val();
        var SelectProjectName = $("#ProjectName").val();
        if (callType == "1") {
            if ($(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('iframe')[0]) {
                if (valueEleId != "" && $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('iframe')[0].contentWindow.$('#' + valueEleId)) {
                    $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('iframe')[0].contentWindow.$('#' + valueEleId).val(SelectProjectId);
                }
                if (textEleId != "" && $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('iframe')[0].contentWindow.$('#' + textEleId)) {
                    $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('iframe')[0].contentWindow.$('#' + textEleId).val(SelectProjectName);
                }
            } else {
                if (valueEleId != "" && $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('#' + valueEleId)) {
                    $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('#' + valueEleId).val(SelectProjectId);
                }
                if (textEleId != "" && $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('#' + textEleId)) {
                    $(parent.document).find("li .lr-frame-iframe,.active")[3].contentWindow.$('#' + textEleId).val(SelectProjectName);
                }
            }
        } else if (callType == "2") {
            if (valueEleId != "" && parent.$("#" + top.layer_form.name + "")[0].contentWindow.$('#' + valueEleId)) {
                parent.$("#" + top.layer_form.name + "")[0].contentWindow.$('#' + valueEleId).val(SelectProjectId);
            }
            if (textEleId != "" && parent.$("#" + top.layer_form.name + "")[0].contentWindow.$('#' + textEleId)) {
                parent.$("#" + top.layer_form.name + "")[0].contentWindow.$('#' + textEleId).val(SelectProjectName);
            }
        }
        learun.layerClose(window.name);

        // 保存成功后才回调
        if (!!callBack) {
            callBack();
        }
    }
    page.init();
}